function! s:leaderf_everything() abort
    if !exists("g:lf_everything_loaded")
        try
            packadd LeaderF-everything
        catch /.*/
        endtry
    endif
    if exists("g:lf_everything_loaded") && g:lf_everything_loaded
        let g:Lf_EverythingEsPath = get(g:, 'Lf_EverythingEsPath', 'es')
        " Define es.exe executable path
        if !executable(g:Lf_EverythingEsPath)
            let g:Lf_EverythingEsPath = get(g:, 'Lf_EverythingEsPath', exepath('es.exe'))
        endif
        " Define which file type should be opened with vim when press enter.
        let g:Lf_EverythingOpenfileFilter = get(g:, 'Lf_EverythingOpenfileFilter', ['txt', 'vim', 'm'])
        " Define es.exe option.
        let g:Lf_EverythingEsOptions = get(g:, 'Lf_EverythingEsOptions', '')
        " Define default search dir, e.g., `D:\`
        let g:Lf_EverythingDefaultDir = get(g:, 'Lf_EverythingDefaultDir', 'D:\')
        nnoremap <Plug>LeaderfEverything :<c-u>LeaderfEverything<cr>
        if !hasmapto('<Plug>LeaderfEverything')
            nmap <F3> <Plug>LeaderfEverything
        endif
        let g:leaderf_keymap = get(g:, 'leaderf_keymap', { 'name': '+leaderf' })
        let g:leaderf_keymap.e = [ ':LeaderfEverything', 'everything' ]
    endif
endfunction

function! s:leaderf_git() abort
    let g:leaderf_git_keymap = get(g:, "leaderf_git_keymap", { 'name': '+git' })
    let g:leaderf_git_keymap.b = [':Leaderf git blame', 'blame']
    let g:leaderf_git_keymap.f = [':Git', 'fugitive']
    let g:leaderf_git_keymap.s = [':Git status', 'status']
    let g:leaderf_git_keymap.d = [':Leaderf git diff', 'diff']
    let g:leaderf_git_keymap.l = [':Leaderf git log', 'log']
    let g:leaderf_keymap = get(g:, 'leaderf_keymap', { 'name': '+leaderf' })
    let g:leaderf_keymap.g = g:leaderf_git_keymap
endfunction

function! s:leaderf_rg() abort
    if executable('rg')
        let g:Lf_DefaultExternalTool = 'rg'
        nnoremap <Plug>LeaderfRgInteractive :<c-u>LeaderfRgInteractive<cr>
        if !hasmapto('<Plug>LeaderfRgInteractive') 
            nmap <F4> <Plug>LeaderfRgInteractive
        endif
    endif
endfunction

function! api#leaderf#init() abort
    call api#pyenv#init()
    " LeaderF" {{{
    if !exists('g:leaderf_loaded')
        try
            packadd LeaderF
            packadd LeaderF-git
            packadd LeaderF-everything
        catch /.*/
        endtry
    endif
    if exists('g:leaderf_loaded') && g:leaderf_loaded
        if !hasmapto('<Plug>LeaderfFileBottom')
            nmap <leader>f <Plug>LeaderfFileBottom
        endif
        if !hasmapto('<Plug>LeaderfBufferBottom') && empty(maparg('<leader>b'))
            nmap <leader>b <Plug>LeaderfBufferBottom
        endif
        nnoremap <Plug>LeaderfHelp :<c-u>LeaderfHelp<cr>
        if !hasmapto('<Plug>LeaderfHelp')
            nmap <leader>? <Plug>LeaderfHelp
        endif
        nnoremap <Plug>LeaderfSelf :<c-u>LeaderfSelf<cr>
        if !hasmapto('<Plug>LeaderfSelf')
            nmap <leader>l <Plug>LeaderfSelf
        endif
        if exists(':LeaderfGstatus')
            nnoremap <Plug>LeaderfGstatus :<c-u>LeaderfGstatus<cr>
            if !hasmapto('<Plug>LeaderfGstatus') " && empty(maparg('<leader>g'))
                nmap <C-g> <Plug>LeaderfGstatus
            endif
        endif
        call s:leaderf_everything()
        call s:leaderf_git()
        let g:leaderf_keymap = get(g:, 'leaderf_keymap',    { 'name': '+leaderf' })
        let g:leaderf_keymap.b = [ ':LeaderfBuffer',        'buffer' ]
        let g:leaderf_keymap.c = [ ':LeaderfCommand',       'command' ]
        let g:leaderf_keymap.f = [ ':LeaderfFile',          'file' ]
        let g:leaderf_keymap.F = [ ':LeaderfFunction',      'function' ]
        let g:leaderf_keymap.G = [ ':LeaderfRgInteractive', 'grep' ]
        let g:leaderf_keymap.h = [ ':LeaderfHelp',          'help' ]
        let g:leaderf_keymap.q = [ ':LeaderfQuickFix',      'quickfix' ]
        let g:leaderf_keymap.r = [ ':LeaderfMru',           'recent' ]
        let g:leaderf_keymap.s = [ ':LeaderfSelf',          'self' ]
        let g:leaderf_keymap.w = [ ':LeaderfWindow',        'window' ]
        let g:space_keymap = get(g:, 'space_keymap', { 'name': '+space' })
        let g:space_keymap.l = g:leaderf_keymap
        " buffer
        let g:buffer_keymap = get(g:, "buffer_keymap", { "name": "+buffer" })
        let g:buffer_keymap.b = [ ':LeaderfBuffer',         'buffer' ]
        " file_keymap
        let g:file_keymap = get(g:, "file_keymap", { "name": "+file" })
        let g:file_keymap.b = [ ':LeaderfBuffer',           'buffer' ]
        let g:file_keymap.f = [ ':LeaderfFile',             'file' ]
        let g:file_keymap.g = [ ':LeaderfRgInteractive',    'grep' ]
        let g:file_keymap.l = [ ':call api#enter#on_vim()', 'ultisnips' ]
        let g:file_keymap.r = [ ':LeaderfMru',              'recent' ]
        let g:file_keymap.u = [ ':Unite ultisnips',         'ultisnips' ]
    endif
    " }}}
endfunction

